package com.kaikeba.servlet.handler;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaikeba.servlet.handler.adapter.HandlerAdapter;
import com.kaikeba.servlet.handler.adapter.UserAddAdapeter;
import com.kaikeba.servlet.handler.adapter.UserDelAdapeter;
import com.kaikeba.servlet.handlermapping.HandlerMapping;
import com.kaikeba.servlet.handlermapping.impl.BeanUrlHandlerMapping;
import com.kaikeba.servlet.handlermapping.impl.SimpleHandlerMapping;
import com.kkb.springframework.beans.aware.BeanFactoryAware;
import com.kkb.springframework.beans.factory.BeanFactory;
import com.kkb.springframework.beans.factory.DefaultListableBeanFactory;

@SuppressWarnings("serial")
public class DispatcherServlet extends AbstractHttpServlet{
	
	private List<HandlerMapping> handlerMapping=new ArrayList<>();
	private List<HandlerAdapter> adapterList=new ArrayList<>();
	private BeanFactory beanFactory;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
//		handlerList.add(new BeanUrlHandlerMapping());
//		handlerList.add(new SimpleHandlerMapping());
//		adapterList.add(new UserAddAdapeter());
//		adapterList.add(new UserDelAdapeter());
		String location = config.getInitParameter("contextConfigLocation");
		//创建spring-ioc容器
		beanFactory=new DefaultListableBeanFactory(location);
		//创建所有的Bean
		beanFactory.getBeansByType(Object.class);
		//将对应类型的bean放入指定集合
		handlerMapping = beanFactory.getBeansByType(HandlerMapping.class);
		adapterList=beanFactory.getBeansByType(HandlerAdapter.class);
	}

	@Override
	public void doDispatch(HttpServletRequest req, HttpServletResponse resp) {
		//step1:获取URL
		//String url = req.getRequestURI();
		//step2:根据URL获取对应的处理器
		Object handler = getHandler(req);
		//step3:执行处理器对应的方法
//		if(handler instanceof UserAddHandler){
//			 ((UserAddHandler) handler).addUser(req, resp);
//		}else if(handler instanceof UserDelHandler){
//			((UserDelHandler) handler).delUser(req, resp);
//		}else if(handler instanceof HttpServletHandler){
//			((HttpServletHandler) handler).handlerRequest(req, resp);
//		}
		HandlerAdapter adapter = getHandlerAdapter(handler);
		if(adapter!=null){
			adapter.dohandlerRequest(handler, req, resp);
		}
		//step4:对处理结果进行响应

	}
	
	/**
	 * 通过策略模式来获取处理器的适配器
	 * @param req
	 * @return
	 */
	private HandlerAdapter getHandlerAdapter(Object handler) {
		for (HandlerAdapter handlerAdapter : adapterList) {
			boolean isSuport = handlerAdapter.isSuport(handler);
			if(isSuport){
				return handlerAdapter;
			}
		}
		return null;
	}

	/**
	 * 通过策略模式查找请求URI对应的handler
	 * @param request
	 * @return
	 */
	private Object getHandler(HttpServletRequest request) {
		for (HandlerMapping handlerMapping : handlerMapping) {
			Object handler = handlerMapping.getHandler(request);
			if(handler!=null){
				return handler;
			}
		}
		return null;
	}


}
